/**********************************************
*  Classes:   ParticleSystemManager
*  Desc:      Singleton class. Manages Particle
*			  Systems
*  Author:    Vaios Kalpias-Ilias (C) 2008-2009
***********************************************/

#include "ParticleSystemManager.h"


//----------------------------------------------------
ParticleSystemManager* ParticleSystemManager::m_spInstance = 0;
//----------------------------------------------------
/**
* Ctor
*/
ParticleSystemManager::ParticleSystemManager()
{
}
//----------------------------------------------------
/**
* Dtor
*/
ParticleSystemManager::~ParticleSystemManager()
{
	Shutdown();
}
//----------------------------------------------------
/**
* Gets instance
*/
ParticleSystemManager* ParticleSystemManager::GetInstance()
{
	if (!m_spInstance)
	{
		m_spInstance = new ParticleSystemManager();
	}

	return m_spInstance;

}
//----------------------------------------------------
/**
* Destroys instance
*/
void ParticleSystemManager::Destroy()
{
	if (m_spInstance)
	{
		delete m_spInstance;
	}
}
//----------------------------------------------------
/**
* Initializes manager
*/
bool ParticleSystemManager::Init()
{
	return true;
}
//----------------------------------------------------
/**
* Resets manager
*/
void ParticleSystemManager::Reset()
{
	std::list<ParticleSystemPtr>::iterator it;
	for (it = m_lSystems.begin();
		it != m_lSystems.end();
		++it)
	{
		(*it)->Reset();
	}
}
//----------------------------------------------------
/**
* Adds system
*/
void ParticleSystemManager::AddSystem(ParticleSystemPtr system)
{
	if (system)
		m_lSystems.push_back(system);
}
//----------------------------------------------------
/**
* Removes system
*/
void ParticleSystemManager::RemoveSystem(ParticleSystemPtr system)
{
	if (system)
	{
		m_lSystems.remove(system);		
	}
}
//----------------------------------------------------
/**
* moves all particles
*/
void ParticleSystemManager::MoveParticles(float dt)
{
	std::list<ParticleSystemPtr>::iterator it;
	for (it = m_lSystems.begin();
		it != m_lSystems.end();
		++it)
	{
		(*it)->MoveParticles(dt);
	}
}
//----------------------------------------------------
/**
* Updates all particle systems
*/
void ParticleSystemManager::UpdateSystems()
{
	std::list<ParticleSystemPtr>::iterator it =
		m_lSystems.begin();

	while (it != m_lSystems.end())
	{
		std::list<ParticleSystemPtr>::iterator next = it;
		++next;  

		if ((*it)->Dead())
		{
			(*it)->DetachParent();
			m_lSystems.erase(it);
		}
		
		it = next;	
	}
	
}
//----------------------------------------------------
/**
* Shuts down manager
*/
void ParticleSystemManager::Shutdown()
{
	std::list<ParticleSystemPtr>::iterator it;
	for (it = m_lSystems.begin();
		it != m_lSystems.end();
		++it)
	{
		//(*it)->DetachParent();
	}
	m_lSystems.clear();
}